<template>
  <el-dialog
    v-model="visible"
    :title="$t('user.updateGrowth')"
    width="30%"
  >
    <el-form
      ref="dataFormRef"
      :model="dataForm"
      :rules="dataRule"
      :label-width="$t('language') === 'English' ? '160px' : '100px'"
      @submit.prevent
    >
      <el-form-item
        :label="$t('user.growthTip2')"
        prop="growthValue"
      >
        <el-input
          v-model.number="dataForm.growthValue"
          type="number"
        />
        <span>{{ $t('user.growthTip1') }}</span>
      </el-form-item>
      <el-form-item>
        <div
          class="default-btn"
          @click="visible = false"
        >
          {{ $t('remindPop.cancel') }}
        </div>
        <div
          class="default-btn primary-btn"
          :loading="confirmLoad"
          @click="onConfirm"
        >
          {{ $t('remindPop.confirm') }}
        </div>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script setup>
import { ElMessage } from 'element-plus'
const emit = defineEmits(['refreshDataList'])
// eslint-disable-next-line no-unused-vars
const validateNumber = (rule, value, callback) => {
  if (!/^(-)?[1-9]\d*$/.test(value)) {
    callback(new Error($t('user.growthTip3')))
  } else if (value > 2147483640 || value < -2147483640) {
    callback(new Error($t('user.balanceTip4')))
  } else {
    callback()
  }
}
const dataRule = {
  growthValue: [
    { required: true, message: $t('user.growthTip4'), trigger: 'blur' },
    { validator: validateNumber, trigger: 'blur' }
  ]
}
const visible = ref(false)
const dataForm = ref({
  growthValue: null,
  userIds: []
})
const init = ids => {
  visible.value = true
  dataForm.value.userIds = ids
  dataForm.value.growthValue = null
}
const confirmLoad = ref(false)
const dataFormRef = ref(null)
const onConfirm = () => {
  dataFormRef.value.validate(valid => {
    if (!valid) {
      return
    }
    if (!dataForm.value.userIds) {
      return
    }
    confirmLoad.value = true
    const param = dataForm.value
    http({
      url: http.adornUrl('/user/userLevel/updateGrowth'),
      method: 'put',
      data: http.adornData(param)
    }).then(() => {
      ElMessage({
        message: $t('remindPop.success'),
        type: 'success',
        duration: 1500,
        onClose: () => {
          visible.value = false
          confirmLoad.value = false
          emit('refreshDataList')
        }
      })
    })
  })
}
defineExpose({ init })
</script>
